home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / Processes / SubLaunch / sublaunch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  11.4 KB  |  427 lines  |  [TEXT/MPS ]

  1. /*______________________________________________________*/
  2. /*                            C Shell                        */
  3. /*                          by                          */
  4. /*                  RICHARD P. COLLYER                  */
  5. /*                       09/02/87                       */
  6. /*______________________________________________________*/
  7.  
  8. #include    <CType.h>
  9. #include    <Quickdraw.h>
  10. #include    <Windows.h>
  11. #include    <OSUtils.h>
  12. #include    <Controls.h>
  13. #include    <desk.h>
  14. #include    <dialogs.h>
  15. #include    <Events.h>
  16. #include    <Memory.h>
  17. #include    <Menus.h>
  18. #include    <Notification.h>
  19. #include    <OSEvents.h>
  20. #include    <Packages.h>
  21. #include    <Palette.h>
  22. #include    <Resources.h>
  23. #include    <SANE.h>
  24. #include    <SegLoad.h>
  25. #include    <Sound.h>
  26. #include    <ToolUtils.h>
  27. #include    <StdIO.h>
  28. #include    <math.h>
  29.  
  30. extern _DataInit();
  31.  
  32. #define    TRUE            0xFF
  33. #define    FALSE            0
  34. #define    VERSION            1
  35. #define    SR_BIT            0
  36.  
  37. #define    appleID            128            
  38. #define    appleMenu        0
  39. #define    aboutMeCommand    1
  40.  
  41. #define    fileID            129
  42. #define    startCommand    1
  43. #define    quitCommand     2
  44.  
  45. #define    editID            130
  46.  
  47. #define    aboutMeDLOG        128
  48. #define    okButton        1
  49. #define    authorItem        2
  50. #define    languageItem    3
  51.  
  52. #define    nocolorID        130
  53. #define    no68020            133
  54. #define    no68881            132
  55. #define    no256            134
  56. #define    nosys6            135
  57.  
  58. typedef struct LaunchStruct {
  59.     char                  *pfName;     /* pointer to the name of launchee */
  60.     short int        param;
  61.     char            LC[2];     /*extended parameters:*/
  62.     long int        extBlockLen;/*number of bytes in extension == 6*/
  63.     short int        fFlags;     /*Finder file info flags (see below)*/
  64.     long int        launchFlags; /*bit 31,30==1 for sublaunch, others                             reserved*/
  65. } *pLaunchStruct;
  66.  
  67. pascal OSErr Launchit(pLaunchStruct pLnch) /* < 0 means error */
  68.     = {0x205F, 0xA9F2, 0x3E80};
  69.  
  70. long                Tick;
  71. int                    yieldTime, err, numcolor, offLeft, offTop, offRight, offBottom;
  72. Rect                screenRect, BaseRect, TotalRect, minRect;
  73. WindowPtr            whichWindow, myWindow;
  74. CTabHandle            mycolors;
  75. PaletteHandle        srcPalette;
  76. MenuHandle            mymenu1, mymenu2, mymenu0;
  77. EventRecord         myEvent;
  78. Boolean                DoneFlag;
  79. GDHandle            theGDevice;
  80. SysEnvRec            theWorld;
  81. OSErr                OSys;
  82.     
  83. /*______________________________________________________*/
  84. /*                What ever You want                    */
  85. /*______________________________________________________*/
  86. OSErr DoLaunch()
  87.  
  88. {  /* DoLaunch */       
  89.     struct LaunchStruct    myLaunch;
  90.     Point                wher;             /*where to display dialog*/
  91.     SFReply                   reply;           /*reply record*/
  92.     SFTypeList           myFileTypes;    /* we only want APPLs */        
  93.     short int               numFileTypes=1;
  94.     HFileInfo            myPB;
  95.     char                   *dirNameStr;
  96.     OSErr                err;
  97.     wher.h = 80;
  98.     wher.v = 90;
  99.     myFileTypes[0] = 'APPL'; /* we only want APPLs */ 
  100.     /*Let the user choose the file to Launch*/
  101.     SFGetFile(wher, "", nil, numFileTypes, myFileTypes, nil, &reply);
  102.  
  103.     if (reply.good)  
  104.     {
  105.         dirNameStr = &reply.fName;  /*initialize to file selected*/
  106.     
  107.     /*Get the Finder flags*/
  108.         myPB.ioNamePtr= dirNameStr;
  109.         myPB.ioVRefNum= reply.vRefNum;
  110.         myPB.ioFDirIndex= 0;
  111.         myPB.ioDirID = 0;
  112.           err = PBGetCatInfo((CInfoPBPtr) &myPB,false);
  113.         if (err != noErr)
  114.             return err;
  115.     
  116.     /*Set the current volume to where the target application is*/
  117.           err = SetVol(nil, reply.vRefNum);
  118.         if (err != noErr)
  119.             return err;
  120.     
  121.     /*Set up the launch parameters*/
  122.         myLaunch.pfName = &reply.fName; /*pointer to our fileName*/
  123.         myLaunch.param = 0; /*we don’t want alternate screen or sound                         buffers*/
  124.     /*set up LC so as to tell Launch that there is non-junk next*/
  125.          myLaunch.LC[0] = 'L'; myLaunch.LC[1] = 'C';
  126.          myLaunch.extBlockLen = 6; /*length of param. block past this                             long word*/
  127.     /*copy flags; set bit 6 of low byte to 1 for RO access:*/
  128.          myLaunch.fFlags = myPB.ioFlFndrInfo.fdFlags; /*from                                                   GetCatInfo*/
  129.          myLaunch.launchFlags = 0xC0000000; /*set BOTH hi bits for a                                 sublaunch*/
  130.     
  131.         err = Launchit(&myLaunch); /* do the sublaunch */
  132.         if (err < 0)
  133.         {
  134.         /* the launch failed, so put up an alert to inform the user */
  135.             /*DoLaunchFailed();*/
  136.             return err;
  137.         }
  138.         else
  139.             return noErr;
  140.     } /*if reply.good*/
  141. } /*DoLaunch*/
  142.  
  143. /*______________________________________________________*/
  144. /*                 About Prog Dialog                    */
  145. /*______________________________________________________*/
  146. void showAboutMeDialog()
  147. {
  148.     GrafPtr     savePort;
  149.     DialogPtr    theDialog;
  150.     short        itemHit;
  151.  
  152.     GetPort(&savePort);
  153.     theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
  154.     SetPort(theDialog);
  155.  
  156.     do {
  157.         ModalDialog(nil, &itemHit);
  158.     } while (itemHit != okButton);
  159.  
  160.     CloseDialog(theDialog);
  161.  
  162.     SetPort(savePort);
  163.     return;
  164. }
  165.  
  166. /*______________________________________________________*/
  167. /*                 Do Menu Function                     */
  168. /*______________________________________________________*/
  169. void doCommand(mResult)
  170.     long    mResult;
  171. {
  172.     int                     theMenu, theItem;
  173.     char                    daName[256];
  174.     GrafPtr                 savePort;
  175.     OSErr                    err;
  176.  
  177.     theItem = LoWord(mResult);
  178.     theMenu = HiWord(mResult);
  179.     
  180.     switch (theMenu) {
  181. /*______________________________________________________*/
  182. /*                    Do Apple Menu                     */
  183. /*______________________________________________________*/
  184.         case appleID:
  185.             if (theItem == aboutMeCommand)
  186.                 showAboutMeDialog();
  187.             else {
  188.                 GetItem(mymenu0, theItem, daName);
  189.                 GetPort(&savePort);
  190.                 (void) OpenDeskAcc(daName);
  191.                 SetPort(savePort);
  192.             }
  193.             break;
  194. /*______________________________________________________*/
  195. /*                     Do File Menu                     */
  196. /*______________________________________________________*/
  197.         case fileID:
  198.             switch (theItem) {
  199.                 case startCommand:
  200.                     err = DoLaunch();
  201.                     break;
  202.                 case quitCommand:
  203.                     DoneFlag = TRUE;
  204.                     break;
  205.                 default:
  206.                     break;
  207.                 }
  208.             break;
  209. /*______________________________________________________*/
  210. /*                     Do Edit Menu                     */
  211. /*______________________________________________________*/
  212.         case editID:
  213.             switch (theItem) {
  214.                 default:
  215.                     break;
  216.                 }
  217.             break;
  218.         default:
  219.             break;
  220.         }
  221.     HiliteMenu(0);
  222.     return;
  223. }
  224.  
  225. /*______________________________________________________*/
  226. /*               Initialization traps                   */
  227. /*______________________________________________________*/
  228. void init()
  229. {
  230.     RgnHandle        tempRgn;
  231.     
  232.     UnloadSeg(_DataInit);
  233.     InitGraf(&qd.thePort);
  234.     FlushEvents(everyEvent, 0);
  235.     InitWindows();
  236.     InitDialogs(nil);
  237.     InitCursor();
  238. /*______________________________________________________*/
  239. /*            If not right Machine then stop            */
  240. /*______________________________________________________*/
  241.     OSys = SysEnvirons(VERSION,&theWorld);
  242.     /*if(!theWorld.hasColorQD) {
  243.         crashDia = GetNewDialog (nocolorID, nil, (WindowPtr) -1);
  244.         DrawDialog (crashDia);
  245.         Delay (300, tick);
  246.         ExitToShell();
  247.         }
  248.     if(theWorld.processor != env68020) {
  249.         crashDia = GetNewDialog (no68020, nil, (WindowPtr) -1);
  250.         DrawDialog (crashDia);
  251.         Delay (300, tick);
  252.         ExitToShell();
  253.         }
  254.     if(!theWorld.hasFPU) {
  255.         crashDia = GetNewDialog (no68881, nil, (WindowPtr) -1);
  256.         DrawDialog (crashDia);
  257.         Delay (300, tick);
  258.         ExitToShell();
  259.         }
  260.     if(theWorld.systemVersion < 0x0600) {
  261.         crashDia = GetNewDialog (nosys6, nil, (WindowPtr) -1);
  262.         DrawDialog (crashDia);
  263.         Delay (300, tick);
  264.         ExitToShell();
  265.         }*/
  266.             
  267. /*______________________________________________________*/
  268. /*                     Set Rects                        */
  269. /*______________________________________________________*/
  270.     screenRect = qd.screenBits.bounds;
  271.     offLeft = 0;
  272.     offTop = 0;
  273.     offRight = screenRect.right;
  274.     offBottom = screenRect.bottom;
  275.     SetRect(&BaseRect, 40, 60, 472, 282);
  276.     tempRgn = GetGrayRgn();
  277.     HLock ((Handle) tempRgn);
  278.     TotalRect = (**tempRgn).rgnBBox;
  279.     SetRect(&minRect, 80, 80, (**tempRgn).rgnBBox.right - 40, 
  280.                 (**tempRgn).rgnBBox.bottom - 40);
  281.     HUnlock ((Handle) tempRgn);
  282.  
  283. /*______________________________________________________*/
  284. /*        Open Window & set Palette & Picture           */
  285. /*______________________________________________________*/
  286.     theGDevice = GetMainDevice();
  287.     HLock ((Handle) theGDevice);
  288.     mycolors = (**(**theGDevice).gdPMap).pmTable;
  289.     numcolor = (**(**theGDevice).gdPMap).pixelSize;
  290.     HUnlock((Handle) theGDevice);
  291.     switch (numcolor) {
  292.         case 1:
  293.             numcolor = 2;
  294.             break;
  295.         case 2:
  296.             numcolor = 4;
  297.             break;
  298.         case 4:
  299.             numcolor = 16;
  300.             break;
  301.         case 8:
  302.             numcolor = 256;
  303.             break;
  304.         }
  305.     
  306.     myWindow = NewCWindow(nil, &BaseRect, "", TRUE, zoomDocProc, 
  307.                             (WindowPtr) -1, TRUE, 150);
  308.     SetPort((WindowPtr) myWindow);
  309.     DrawGrowIcon (myWindow);
  310.  
  311.     srcPalette = NewPalette (numcolor, mycolors, pmCourteous, 0);
  312.     SetPalette ((WindowPtr) myWindow, srcPalette, TRUE);
  313.     
  314. /*______________________________________________________*/
  315. /*                    Set menus                         */
  316. /*______________________________________________________*/
  317.     mymenu0 = GetMenu(appleID);
  318.     AddResMenu(mymenu0, 'DRVR');
  319.     InsertMenu(mymenu0,0);
  320.     mymenu1 = newmenu(129,"File");
  321.     appendmenu(mymenu1,"Start;Quit");
  322.     InsertMenu(mymenu1,0);
  323.     mymenu2 = newmenu(130,"Edit");
  324.     InsertMenu(mymenu2,0);
  325.     DrawMenuBar();
  326.  
  327. /*______________________________________________________*/
  328. /*                  Init variables                      */
  329. /*______________________________________________________*/
  330.     DoneFlag = FALSE;
  331.     yieldTime = 0;
  332.     return;
  333. }
  334.  
  335. main()
  336. {
  337.     Boolean        track;
  338.     long        growResult;
  339. /*______________________________________________________*/
  340. /*                   Main Event loop                    */
  341. /*______________________________________________________*/
  342.     init();
  343.     for ( ;; ) {
  344.         if (DoneFlag) {
  345.             ExitToShell();
  346.             }
  347.         if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
  348.             switch (myEvent.what) {
  349.                 case mouseDown:
  350.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  351.                         case inSysWindow:
  352.                             SystemClick(&myEvent, whichWindow);
  353.                             break;
  354.                         case inMenuBar:
  355.                             doCommand(MenuSelect(myEvent.where));
  356.                             break;
  357.                         case inContent:
  358.                             break;
  359.                         case inDrag:
  360.                             DragWindow (whichWindow, myEvent.where, &TotalRect);
  361.                             EraseRect (&whichWindow->portRect);
  362.                             DrawGrowIcon (whichWindow);
  363.                             break;
  364.                         case inGrow:
  365.                             growResult = GrowWindow (whichWindow, myEvent.where,
  366.                                                     &minRect);
  367.                             SizeWindow(whichWindow, LoWord(growResult), 
  368.                                     HiWord(growResult), TRUE);
  369.                             EraseRect (&whichWindow->portRect);
  370.                             DrawGrowIcon (whichWindow);
  371.                             break;
  372.                         case inGoAway:
  373.                             track = TrackGoAway (whichWindow, myEvent.where);
  374.                             if (track)
  375.                                 CloseWindow (whichWindow);
  376.                             break;
  377.                         case inZoomIn:
  378.                             track = TrackBox (whichWindow, myEvent.where, inZoomIn);
  379.                             if (track) {
  380.                                 ZoomWindow (whichWindow, inZoomIn, TRUE);
  381.                                 EraseRect (&whichWindow->portRect);
  382.                                 DrawGrowIcon (whichWindow);
  383.                                 }
  384.                             break;
  385.                         case inZoomOut:
  386.                             track = TrackBox (whichWindow, myEvent.where, inZoomOut);
  387.                             if (track) {
  388.                                 ZoomWindow (whichWindow, inZoomOut, TRUE);
  389.                                 EraseRect (&whichWindow->portRect);
  390.                                 DrawGrowIcon (whichWindow);
  391.                                 }
  392.                             break;
  393.                         default:
  394.                             break;
  395.                         }
  396.                     break;
  397.                 case keyDown:
  398.                     break;
  399.                 case autoKey:
  400.                     break;
  401.                 case updateEvt:
  402.                     if ((WindowPtr) myEvent.message == myWindow) {
  403.                         BeginUpdate((WindowPtr) myWindow);
  404.                         EndUpdate((WindowPtr) myWindow);
  405.                         }
  406.                     break;
  407.                 case diskEvt:
  408.                     break;
  409.                 case activateEvt:
  410.                     break;
  411.                 case app4Evt:
  412.                     if ((myEvent.message << 31) == 0) { /* Suspend */
  413.                         yieldTime = 30;
  414.                         HideWindow((WindowPtr) myWindow);
  415.                         }
  416.                     else { /* Resume */
  417.                         yieldTime = 0;
  418.                         ShowWindow((WindowPtr) myWindow);
  419.                         SetPort((WindowPtr) myWindow);
  420.                         }
  421.                     break;
  422.                 default:
  423.                     break;
  424.                 }
  425.             }
  426.         }
  427. }